package com.springboot.framework.controller;

import com.springboot.framework.controller.request.SellerUpdateBean;
import com.springboot.framework.controller.request.ShopUpdateBean;
import com.springboot.framework.controller.request.StatusUpdateBean;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.SellerModel;
import com.springboot.framework.model.ShopModel;
import com.springboot.framework.service.ShopService;
import com.springboot.framework.utils.ResponseVoUtil;
import com.springboot.framework.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author SWF
 * @date 2019/6/14 21:19
 **/
@Api(description = "【管理员】shop")
@RestController
@RequestMapping("/admin/shop")
public class AdminShopController {

    @Resource
    private ShopService shopService;

    @ApiOperation(value = "获取所有商铺", notes = "获取所有商铺")
    @GetMapping("/list")
    @ResponseBody
    public ResponseVO listAll() {
        List<ShopModel> shopModels = shopService.listAll();
        return ResponseVoUtil.success(shopModels);
    }

    @ApiOperation(value = "修改shop信息", notes = "修改shop信息")
    @PostMapping("/update")
    public ResponseVO<ShopModel> update(@RequestBody @Valid ShopUpdateBean bean, BindingResult bindingResult, HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        ShopModel shopModel = new ShopModel();
        BeanUtils.copyProperties(bean,shopModel);
        shopModel = shopService.update(shopModel);
        return ResponseVoUtil.success(shopModel);
    }

    @ApiOperation(value = "修改shop状态", notes = "修改shop状态")
    @PostMapping("/updateStatus")
    public ResponseVO<ShopModel> update(@RequestBody StatusUpdateBean bean, HttpServletRequest request) {
        ShopModel shopModel = new ShopModel();
        shopModel.setId(bean.getId());
        shopModel.setStatus(bean.getStatus());
        shopModel = shopService.updateStatus(shopModel);
        return ResponseVoUtil.success(shopModel);
    }

}
